#include "forgetpassword.h"
#include "ui_forgetpassword.h"

ForgetPassword::ForgetPassword(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ForgetPassword)
{
    ui->setupUi(this);
    this->setWindowTitle("OpenChatRoom");
    this->setFixedSize(850,500);
    QPixmap pixmap=QPixmap(":/source/back.jpg").scaled(this->size());
    QPalette palette;
    palette.setBrush(QPalette::Window,QBrush(pixmap));
    this->setPalette(palette);
    //文本框占位
     ui->idEdit->setPlaceholderText("输入账号查询密保问题");
     ui->answerEdit->setPlaceholderText("密保答案");
     ui->passwordEdit->setPlaceholderText("6位数字或英文大小写");

     connect(ui->backButton,SIGNAL(clicked(bool)),this,SIGNAL(clearAll()));
     connect(ui->backButton,SIGNAL(clicked(bool)),this,SIGNAL(back()));

     //退出时清空重置文本框
     connect(ui->backButton,&QPushButton::clicked,[this]()
             {

              //输入框只读状态解除
                QLineEdit *idLEdit = ui->idEdit;
                QLineEdit *answerLEdit = ui->answerEdit;

                 idLEdit->setReadOnly(false);
                 answerLEdit->setReadOnly(false);

                 //退出时清空文本框
                 ui->idEdit->clear();
                 ui->questionEdit->clear();
                 ui->answerEdit->clear();
                 ui->passwordEdit->clear();
                 flag1 = 0;
                 flag2 = 0;
             });



    //连接云数据库
//    db = QSqlDatabase::addDatabase("QMYSQL"); //加载mysql数据库驱动
//    db.setHostName("47.115.215.143");
//    db.setPort(3307);
//    db.setDatabaseName("YUNMYSQL");
//    db.setUserName("root");
//    db.setPassword("123456");
//    db.open();
    //db.open()有效
//    if(db.open())
//    {
//        QMessageBox::information(this,"连接提示","连接成功");
//    }
//    else
//    {
//        QMessageBox::warning(this,"连接提示","连接失败");
//    }
}

ForgetPassword::~ForgetPassword()
{
    delete ui;
    //db.close();
}

//void ForgetPassword::paintEvent(QPaintEvent *event)
void ForgetPassword::paintEvent()
{
    QPainter painter(this);
    painter.drawPixmap(0,0,width(),height(),QPixmap(":/images/back.jpg"));
}


void ForgetPassword::on_findButton_clicked()
{
    //定义文本框有关变量
    QString id = ui->idEdit->text();
    QString answer = ui->answerEdit->text();
    QString pwd = ui->passwordEdit->text();

    //定义跟数据库有关变量
    QString slct_question;
    QString slct_password;
    QString slct_id;
    QString up_password;

    //输入框只读状态定义
    QLineEdit *idLEdit = ui->idEdit;
    QLineEdit *questionLEdit = ui->questionEdit;
    QLineEdit *answerLEdit = ui->answerEdit;

    questionLEdit->setReadOnly(true); //密保问题输入框为只读状态

    //对输入的ID判断
        QSqlQuery query1;
        QString slct_questoin = QString("select RecoverQuestion from User where Uid='%1'").arg(id);
        query1.exec(slct_questoin);


        if(query1.first()){

            slct_question = query1.value(0).toString();
            ui->questionEdit->setText(slct_question);

            idLEdit->setReadOnly(true); //填写正确后，id输入框变为只读状态
            flag1 = 1;

        }else
        {
            QMessageBox::warning(this,"输入提示","账号错误，请重新输入！");
            ui->idEdit->clear();
        }


    //对输入的密保答案判断
    if(flag1){

        if(!ui->answerEdit->text().isEmpty() && ui->passwordEdit->text().isEmpty()){

            QSqlQuery query2;
            slct_password = QString("select * from User where Uid='%1' and RecoverAnswer='%2'").arg(id).arg(answer);
            query2.exec(slct_password);

            if(query2.first()){
                slct_id = query2.value(0).toString();
                QMessageBox::information(this,"输入提示","答案正确，请输入六位新密码！");
               answerLEdit->setReadOnly(true); //填写正确后，密保答案输入框变为只读状态
                flag2 = 1;
            }
            else
            {
                QMessageBox::warning(this,"输入提示","重新输入正确的密保答案！");
                ui->answerEdit->clear();
            }
        }

    }

    //对输入的密码判断
    if(flag2){

        if(!ui->passwordEdit->text().isEmpty()){

            bool exe=false;
            QSqlQuery query3;
            up_password = QString("update User set Password='%1' where Uid='%2'").arg(pwd).arg(id);

            bool right =  IsEnglishOrNumber(pwd);
//            if(right){
//                qDebug()<<"密码格式正确";
//            }

            //判断输入位数是否为6
            if (ui->passwordEdit->text().count()==6 && right){
                exe  = query3.exec(up_password);
            }

            if(exe)
            {
                QMessageBox::information(this,"输入提示","密码修改成功！！");

            }
            else
            {
                QMessageBox::warning(this,"输入提示","密码格式错误！");
                ui->passwordEdit->clear();
            }

        }
    }


}

bool ForgetPassword:: IsEnglishOrNumber(QString qstrSrc){
    QByteArray ba = qstrSrc.toLatin1();
    const char *s = ba.data();
    bool bret = true;
    while(*s)
    {
        if((*s>='A' && *s<='Z') || (*s>='a' && *s<='z') || (*s>='0' && *s<='9'))
        {

        }
        else
        {
            bret = false;
            break;
        }
        s++;
    }
    return bret;
}








